package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class ListPage extends Utils {
    public static String url="http://43.138.73.175:8080/book_list.html";
    public ListPage(){
        super(url);
    }

    public void ListPageRight() throws IOException {
        //通过查看页面元素观察是否加载完成列表页
        driver.findElement(By.cssSelector("body > div > h2"));
        getScreenShot(getClass().getName());
    }
    public void DeleteFail() throws IOException {
        driver.findElement(By.cssSelector("body > div > table > tbody > tr:nth-child(1) > td:nth-child(9) > div > a:nth-child(2)")).click();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        String alertText = alert.getText();
        alert.dismiss();
        getScreenShot(getClass().getName());
        assert alertText.equals("确认删除?");
    }
    public void DeleteSuc() throws IOException {
        driver.findElement(By.cssSelector("body > div > table > tbody > tr:nth-child(1) > td:nth-child(9) > div > a:nth-child(2)")).click();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        String alertText = alert.getText();
        alert.accept();
        assert alertText.equals("确认删除?");
        Alert alert2 = wait.until(ExpectedConditions.alertIsPresent());
        String alertText2 = alert.getText();
        alert2.accept();
        getScreenShot(getClass().getName());
        assert alertText2.equals("删除成功!");
    }
    public void DeletesFail() throws IOException {
        driver.findElement(By.cssSelector("#selectBook")).click();
        driver.findElement(By.cssSelector("body > div > div.navbar-justify-between > div > button:nth-child(2)")).click();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        String alertText = alert.getText();
        alert.dismiss();
        getScreenShot(getClass().getName());
        assert alertText.equals("确认批量删除?");
    }
    public void DeletesSuc() throws IOException {
        driver.findElement(By.cssSelector("body > div > div.navbar-justify-between > div > button:nth-child(2)")).click();
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        Alert alert = wait.until(ExpectedConditions.alertIsPresent());
        String alertText = alert.getText();
        alert.accept();
        assert alertText.equals("确认批量删除?");
        Alert alert2 = wait.until(ExpectedConditions.alertIsPresent());
        String alertText2 = alert.getText();
        alert2.accept();
        getScreenShot(getClass().getName());
        assert alertText2.equals("删除成功!");
        driver.quit();
    }
}
